import {globalConfig} from "../consts/GlobalConfig";
import {shareUtil} from "../../framework/wx/ShareUtil";
import Toast from "../popups/Toast";
import {activityConfig} from "../entity/ActivityConfig";
import {videoAds} from "../../framework/wx/VideoAdsProvider";
import {logger} from "../third-scripts/game_sdk/Logger";
import Wx from "../../framework/wx/Wx";

/**
 * 活动适配类.
 */
export class ActivityAdapter {

    public static get(): ActivityAdapter {
        return this._instance;
    }

    private static _instance: ActivityAdapter = new ActivityAdapter();

    // 启动时的进入场景.
    private queryScene: number = 0;

    constructor() {
        videoAds.setAdId(globalConfig.videoAdsId);
        // 每次判断需要进行一次视频预加载.
        videoAds.preloadAds();
    }

    public setQueryScene(scene: string | number) {
        this.queryScene = +scene;
    }

    // 检测当前应该分享还是看视频.
    public checkCurrentType(scene: string): string {
        const config = globalConfig.acts[scene] || globalConfig.acts["default"];
        let type = config.b;

        if (!globalConfig.videoEnable) {
            // 禁用视频时,全部切为分享.
            type = "s";
        } else {
            if (type == "s") {
                // 分享用完了,就切回视频.
                if (activityConfig.get(scene, "s") >= config.s) {
                    type = "v";
                }
            }
            // 如果判断为当前需要进行视频时.还要再进行检测.
            if (type == "v") {
                videoAds.setAdId(globalConfig.videoAdsId);
                // 每次判断需要进行一次视频预加载.
                videoAds.preloadAds();
                // 视频用完了,就切回分享.
                if (videoAds.isDisabled() || activityConfig.get(scene, "v") >= config.v) {
                    type = "s";
                }
            }
        }
        return type;
    }

    // 进行分享或者看视频.
    public async shareOrVideo(scene: string, params?: any): Promise<{ type: string, success: Boolean }> {
        if (!Wx.isWeChatEnv()) {
            return Promise.resolve({type: "s", success: true});
        }
        let type = this.checkCurrentType(scene);
        if (!globalConfig.videoEnable) {
            type = "s"
        }
        if (type == "s") {
            // 如果不允许分享,则直接返回 true.表示分享成功.
            if (!globalConfig.shareEnable) {
                return Promise.resolve({type: "s", success: true})
            }
            return shareUtil.shareResult(params).then(success => {
                cc.log("分享结果:", success);
                if (success) {
                    this.addSceneCount(scene, type);
                } else {
                    Toast.showToast("分享异常,请分享给好友或群聊天哦~");
                }
                return Promise.resolve({type: "s", success: success});
            });
        } else {
            return new Promise<any>(resolve => {
                videoAds.setOnCloseListener((finish) => {
                    if (finish) {
                        this.addSceneCount(scene, type);
                    } else {
                        Toast.showToast("需要观看完整视频才能获得奖励哦~");
                    }
                    resolve({type: "v", success: finish});
                });
                videoAds.showAds();
            });
        }
    }

    private addSceneCount(scene: string, type: string) {
        activityConfig.add(scene, type);
        // 来回切换视频/分享.
        const config = globalConfig.acts[scene] || globalConfig.acts["default"];
        if (config.mode || globalConfig.acts.mode) {
            config.b = config.b == "s" ? "v" : "s";
        }
    }
}